package com.justdoit.facedemo.utils;

import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.MalformedURLException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


/**
 *  用来进行 二进制、file、MultipartFile、Base64 互相转换的
 * @author: He Wentao
 * @date: 2022/6/22 15:52
 */
public class FileConverter {

    /**
     * MultipartFile 转 File
     *
     * @param file 原始 MultipartFile 类型文件
     * @throws Exception 处理失败
     */
    public static File multipartFileToFile(MultipartFile file) throws Exception {
        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            toFile = inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }

    private static File inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将图片内容处理成Base64编码格式
     *
     * @param file 二进制数据
     * @return 进行Base64编码之后的字符串
     */
    public static String encodeImg(MultipartFile file) {
        byte[] imgBytes = null;
        try {
            imgBytes = file.getBytes();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return imgBytes == null ? null : encoder.encode(imgBytes);
    }



    /**
     * File转Byte[]
     * @param file file 文件
     * @return    byte数组
     */
    public static byte[] fileToBinArray(File file){
        try {
            InputStream fis = new FileInputStream(file);
            byte[] bytes = FileCopyUtils.copyToByteArray(fis);
            return bytes;
        }catch (Exception ex){
            throw new RuntimeException("transform file into bin Array 出错",ex);
        }
    }


    /**
     * byte[]转File
     * @param b 二进制数据
     * @param outputFileName  输出的文件名
     * @return  返回 file 对象
     */
    public static File getFileFromBytes(byte[] b, String outputFileName){
        BufferedOutputStream stream = null;
        File file = null;
        try {
            file = new File("Tempalte",outputFileName);
            FileOutputStream fileStream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fileStream);
            stream.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return file;
    }
}
